﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Windows;

namespace Coding4Fun.Chair.Cockpit
{
    public static class Utilities
    {
        private static double _topBoundary;
        private static double _bottomBoundary;
        private static double _leftBoundary;
        private static double _rightBoundary;
        private static double _itemLeft;
        private static double _itemTop;

        public static void SetMargin(DependencyObject container, double left, double top, double leftOffset, double topOffset)
        {
            if (container == null)
                return;
            
            container.SetValue(FrameworkElement.MarginProperty, new Thickness(left + leftOffset, top + topOffset, 0, 0)); 
        }


        public static void SetMargin(DependencyObject container, double left, double top)
        {
            SetMargin(container, left, top, 0, 0); 
        }

        public static bool IsItemMidpointInContainer(FrameworkElement container, FrameworkElement target)
        {
            FindValues(container, target);

            if (_itemTop < _topBoundary || _bottomBoundary < _itemTop)
            {
                //Midpoint of target is outside of top or bottom
                return false; 
            }
        	
			if (_itemLeft < _leftBoundary || _rightBoundary < _itemLeft)
        	{
                //Midpoint of target is outside of left or right
        		return false; 
        	}

        	return true;
        }

    	private static void FindValues(FrameworkElement container, FrameworkElement target)
        {
            var containerTopLeft = container.PointToScreen(new Point());
            var itemTopLeft = target.PointToScreen(new Point());

            _topBoundary = containerTopLeft.Y;
            _bottomBoundary = _topBoundary + container.ActualHeight;
            _leftBoundary = containerTopLeft.X;
            _rightBoundary = _leftBoundary + container.ActualWidth;

            //use midpoint of item (width or height divided by 2)
            _itemLeft = itemTopLeft.X + (target.ActualWidth / 2);
            _itemTop = itemTopLeft.Y + (target.ActualHeight / 2);
        }

        private const double OneHundredAndEightyDividedByPi = (180 / Math.PI);

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "x")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "y")]
        public static int ConvertToDegrees(double x, double y)
        {
            //Atan2 has y,x coordinates reversed
            var radians = Math.Atan2(y, x);

            //convert radians to degrees
            return (int)(radians * OneHundredAndEightyDividedByPi);
        }
		
        public static double KeepBounds(double value, double min, double max)
        {
            return Math.Min(Math.Max(value, min), max);
        }

        public static bool CheckBounds(double value, double min, double max)
        {
            return min <= value && max >= value;
        }

    }
}
